package zhaolu.com.example.Delicacy.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import zhaolu.com.example.Delicacy.Fragment.MyFragment;
import zhaolu.com.example.Delicacy.Fragment.OrderFragment;
import zhaolu.com.example.Delicacy.Fragment.OrderFragment2;
import zhaolu.com.example.Delicacy.R;

public class FragmentActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取登录信息
        SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        String phone = sp.getString("username", null);
        String password = sp.getString("password", null);

        bottomNavigationView = findViewById(R.id.bootomNav);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new OrderFragment2()).commit();
        }

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment fragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.order:
                        fragment = new OrderFragment2();
                        break;
                    case R.id.my:
                        fragment = new MyFragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
                return true;
            }
        });
    }
}